// 接单操作
function acceptOrder(orderId) {
    const courierId = 1;
    const url = `/courier/${courierId}/accept-order/${orderId}`;
    fetch(url, {
        method: 'POST'
    })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert('接单成功');
                window.location.reload();
            } else {
                alert('接单失败: ' + data.message);
            }
        })
        .catch(error => {
            console.error('请求出错:', error);
            alert('请求出错，请稍后重试');
        });
}

// 转入收益到个人账户操作
function transferEarnings(earningId) {
    const courierId = 1;
    const url = `/courier/${courierId}/transfer-earnings/${earningId}`;
    fetch(url, {
        method: 'POST'
    })
        .then(response => {
            if (response.ok) {
                alert('收益转入成功');
                window.location.reload();
            } else {
                alert('收益转入失败');
            }
        })
        .catch(error => {
            console.error('请求出错:', error);
            alert('请求出错，请稍后重试');
        });
}

// 注册配送员身份操作
function registerCourier() {
    const name = document.getElementById('name').value;
    const idNumber = document.getElementById('id-number').value;
    const phone = document.getElementById('phone').value;
    const url = '/courier/register';
    const data = {
        name: name,
        idNumber: idNumber,
        phone: phone
    };
    fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    })
        .then(response => {
            if (response.ok) {
                alert('注册成功');
                window.location.reload();
            } else {
                alert('注册失败');
            }
        })
        .catch(error => {
            console.error('请求出错:', error);
            alert('请求出错，请稍后重试');
        });
}

// 注销配送员身份操作
function cancelRegistration() {
    const courierId = 1;
    const url = `/courier/${courierId}/update-audit-status/cancel`;
    fetch(url, {
        method: 'POST'
    })
        .then(response => {
            if (response.ok) {
                alert('注销成功');
                window.location.reload();
            } else {
                alert('注销失败');
            }
        })
        .catch(error => {
            console.error('请求出错:', error);
            alert('请求出错，请稍后重试');
        });
}